//
//  IFViewController.m
//  IFJPEGSDK_OBJC
//
//  Created by 赵曙诚 on 10/13/2021.
//  Copyright (c) 2021 赵曙诚. All rights reserved.
//

#import "IFViewController.h"
#import "Masonry.h"

#import <IJEPGOBJCFramework/IJEPGOBJCFramework.h>
//#import "IFJPEGParser.h"

NS_INLINE NSString *cpFileToSandBox(NSString *mainBundleFilePath , NSString *ext)
{
    //copy to sandbox
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fileName = [NSString stringWithFormat:@"%@.%@" , mainBundleFilePath , ext];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:mainBundleFilePath ofType:ext];
    NSString *cpFilePath = [documentPath stringByAppendingPathComponent:[@"if_" stringByAppendingString:fileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:cpFilePath]) {
        [fileManager removeItemAtPath:cpFilePath error:nil];
    }
    
    NSError *error = nil;
    [fileManager copyItemAtPath:filePath toPath:cpFilePath error:&error];
    if (error) {
        NSLog(@"error ====> %@" , error);
    }
    return cpFilePath;
}

@interface IFViewController ()
{
    NSString *testFilePath;
    NSString *irFilePath;
    NSString *tempFilePath;
    NSString *vlFilePath;
}

@property (nonatomic , strong) UIButton *packageButton;
@property (nonatomic , strong) UIButton *unpackageButton;
@property (nonatomic , strong) UIButton *unpackageExportButton;

@end

@implementation IFViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self copyImagesToSandbox];
    [self initUI];
}


-(void)copyImagesToSandbox
{
    testFilePath = cpFileToSandBox(@"org_test" , @"jpg");
    irFilePath = cpFileToSandBox(@"test_image" , @"bin");
    tempFilePath = cpFileToSandBox(@"test_temp" , @"bin");
    vlFilePath = cpFileToSandBox(@"test_jpeg" , @"jpg");
}

-(void)initUI
{
    _packageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _packageButton.backgroundColor = UIColor.blueColor;
    _packageButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [_packageButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [_packageButton addTarget:self action:@selector(actionForPackage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_packageButton];
    [_packageButton setTitle:NSLocalizedString(@"测试IJPEG PACK流程", nil) forState:UIControlStateNormal];
    [_packageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    _unpackageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _unpackageButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [_unpackageButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    _unpackageButton.backgroundColor = UIColor.blueColor;
    [_unpackageButton addTarget:self action:@selector(actionForUnpackage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_unpackageButton];
    [_unpackageButton setTitle:NSLocalizedString(@"测试IJPEG UNPACK流程,不包含图片信息", nil) forState:UIControlStateNormal];
    [_unpackageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(_packageButton.mas_bottom).offset(20);
    }];
    
    _unpackageExportButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _unpackageExportButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [_unpackageExportButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    _unpackageExportButton.backgroundColor = UIColor.blueColor;
    [_unpackageExportButton addTarget:self action:@selector(actionForUnpackageExportedImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_unpackageExportButton];
    [_unpackageExportButton setTitle:NSLocalizedString(@"测试IJPEG UNPACK流程,含图片信息(并且导出图片)", nil) forState:UIControlStateNormal];
    [_unpackageExportButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(_unpackageButton.mas_bottom).offset(20);
    }];
}

-(void)viewSafeAreaInsetsDidChange
{
    [super viewSafeAreaInsetsDidChange];
    [_packageButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20+self.view.safeAreaInsets.top);
    }];
}

#pragma -mark Button Action

-(void)actionForPackage
{
    IFJPEGAllData *packedData = [IFJPEGAllData new];
    // APP2
    // 图片信息
    packedData.ijpegVersion = @"192.236.1.255";
    packedData.ijpegSign = @"IJPEG";
    packedData.imageOrgType = IFImageOrgTypeIR;
    packedData.imageDispType = IFImageDispTypeIR;
    packedData.imageRotate = IFImageRotateZero;
    packedData.imageMirFlip = IFImageMirFlipOriginal;
    packedData.imageColorSwitchable = IRImageColorSwitchableOff;
    packedData.imageCurColor = @(1);
    
    // 红外数据信息 (这个值是2^64-1，即64位的最大值)
    // 256 * 192 * 2
    packedData.irDataInfo.dataSize = @(256 * 192 * 2);
    packedData.irDataInfo.dataFormat = @(1);
    packedData.irDataInfo.width = @(256);
    packedData.irDataInfo.height = @(192);
    packedData.irDataInfo.bitNum = @(16);
    packedData.irDataInfo.dataInfoReserved = @(1);

    // 温度数据信息
    packedData.tempDataInfo.dataSize = @(256 * 192 * 2);
    packedData.tempDataInfo.dataFormat = @(1);
    packedData.tempDataInfo.width = @(256);
    packedData.tempDataInfo.height = @(192);
    packedData.tempDataInfo.bitNum = @(16);
    packedData.tempDataInfo.dataInfoReserved = @(1);

    // 可见光图片信息
    packedData.vlDataInfo.dataSize = @(30699);
    packedData.vlDataInfo.dataFormat = @(1);
    packedData.vlDataInfo.width = @(500);
    packedData.vlDataInfo.height = @(355);
    packedData.vlDataInfo.bitNum = @(16);
    packedData.vlDataInfo.dataInfoReserved = @(1);

    //设置原始数据
    /*
     [packedData setIrOriginDataFilePath:irFilePath];
     [packedData setTempOriginDataFilePath:tempFilePath];
     [packedData setVLOriginDataFilePath:vlFilePath];
      以上三个方法如果原始数据为本地文件可以传入路径
    */
    
    //原始数据（NSData）
    packedData.tempOriginData = [NSData dataWithContentsOfFile:tempFilePath];
    packedData.irOriginData = [NSData dataWithContentsOfFile:irFilePath];
    packedData.vlOriginData = [NSData dataWithContentsOfFile:vlFilePath];

    // APP4 出厂测温参数
    // 默认测温参数
    packedData.factDefaultTempInfo.ems = @1;
    packedData.factDefaultTempInfo.tau = @1;
    packedData.factDefaultTempInfo.ta = @300;
    packedData.factDefaultTempInfo.tu = @300;
    packedData.factDefaultTempInfo.dist = @1;
    packedData.factDefaultTempInfo.A0 = @729;
    packedData.factDefaultTempInfo.B0 = @-4294;
    packedData.factDefaultTempInfo.A1 = @6;
    packedData.factDefaultTempInfo.B1 = @-1265;
    
    // 实时参数
    packedData.factRealTimeTempInfo.sensorTemp = @693;
    packedData.factRealTimeTempInfo.shutterTemp = @693;
    packedData.factRealTimeTempInfo.lensTemp = @112;
    
    // 状态信息
    packedData.factStatusTempInfo.envCorrectStatus = @0;
    packedData.factStatusTempInfo.distCorrectStatus = @1;
    packedData.factStatusTempInfo.tempMapStatus = @1;
    packedData.factStatusTempInfo.gainStatus = @1;
    
    // 标定数据
    packedData.factDistTempInfo.distTableVersion = @"192.168.2.255";
    packedData.factDistTempInfo.distTable = @[@12, @0, @256, @129];
    packedData.factDistTempInfo.nucTTable = @[@666, @6535, @6553, @413];
    packedData.factDistTempInfo.P0 = @18;
    packedData.factDistTempInfo.P1 = @124;
    packedData.factDistTempInfo.P2 = @113;

    // APP5 用户参数
    packedData.userTempInfo.envTemp = @25.0f;
    packedData.userTempInfo.dist = @5.0f;
    packedData.userTempInfo.ems = @0.95f;
    packedData.userTempInfo.hum = @0.5f;
    packedData.userTempInfo.refTemp = @25.0f;

    // 测温信息
    packedData.calTempInfo.tempUnit = @2;
    packedData.calTempInfo.centerTemp = @1;
    packedData.calTempInfo.maxTemp = @0;
    packedData.calTempInfo.minTemp = @1;
    // 点、线、框、圆个数
    packedData.calTempInfo.tempMeasureNum = @3;
    
    
    // 点、线、框、圆数据
    NSMutableArray *tempMeasures = [NSMutableArray new];
    for (int index = 0; index < packedData.calTempInfo.tempMeasureNum.unsignedIntValue; index++) {
        IFTempMeasureInfo *info = [IFTempMeasureInfo new];
        info.type = @1;
        info.label = [NSString stringWithFormat:@"label+%@" , @(index).stringValue];
        info.x1 = @(12+index);
        info.y1 = @(25+index);
        info.x2_or_r1 = @(35 + index);
        info.y2_or_r2 = @(56 + index);
        info.dist = @(23.6f+index);
        info.ems = @(16.9f+index);
        info.refTemp = @(56.9f+index);
        [tempMeasures addObject:info];
    }

    packedData.tempMessureInfo = tempMeasures;
    
    // APP6 融合参数
    packedData.mixParam.mixType = @1;
    packedData.mixParam.correctionAsix = @[@25.2f, @36.9f, @38.1f, @56.4f, @6.8f];

    // APP7设备信息（红外 & 可见光）
    packedData.irCameraInfo.manufacturer = @"ir_manufacturer";
    packedData.irCameraInfo.desc = @"ir_desc";
    packedData.irCameraInfo.pn = @"ir_pn";
    packedData.irCameraInfo.sn = @"ir_sn";
    packedData.irCameraInfo.fwVersion = @"ir_fw_version";
    packedData.irCameraInfo.apertureValue = @5;
    packedData.irCameraInfo.focalLen = @6;

    packedData.vlCameraInfo.manufacturer = @"vl_manufacturer";
    packedData.vlCameraInfo.desc = @"vl_desc";
    packedData.vlCameraInfo.pn = @"vl_pn";
    packedData.vlCameraInfo.sn = @"vl_sn";
    packedData.vlCameraInfo.fwVersion = @"vl_fw_version";
    packedData.vlCameraInfo.apertureValue = @45;
    packedData.vlCameraInfo.focalLen = @18;
    
    IFIJPEGStatus status = [IFJPEGSDK packageJPEGWithFilePath:testFilePath bean:packedData];
    NSLog(@"package status ==> %li" ,(long)status);
    
}

-(void)actionForUnpackage
{
    IFIJPEGStatus status = [IFJPEGSDK unpackageJPEGWithFilePath:testFilePath
                                                   withoutImage:true
                                                   exportImages:false completion:^(IFJPEGAllData * _Nonnull bean, NSString * _Nonnull exportedFilePath) {

    }];
    NSLog(@"unpackage status ==> %li" ,(long)status);
}

-(void)actionForUnpackageExportedImage
{
    IFIJPEGStatus status = [IFJPEGSDK unpackageJPEGWithFilePath:testFilePath
                                                   withoutImage:false
                                                   exportImages:true
                                                     completion:^(IFJPEGAllData * _Nonnull bean, NSString * _Nonnull exportedFilePath) {
        [self shareSystemFile:exportedFilePath];
    }];
    NSLog(@"unpackage exported image status ==> %li" ,(long)status);
}

//分享
-(void)shareSystemFile:(NSString *)shareFilePath
{
    UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[[NSURL fileURLWithPath:shareFilePath]] applicationActivities:nil];
    [self presentViewController:activity animated:YES completion:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end
